//
//  KLHotelDetailViewController.m
//  KunLun
//
//  Created by Mac on 2017/11/28.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "KLHotelDetailViewController.h"
#import "KLHotaldetailTableViewCell.h"
#import "NSCalendar+STCalendarTool.h"
#import "KLReserveHotelViewController.h"
#import "STAdvertingScrollView.h"
#import "KLHotleModel.h"
#import "STPhotoUrlImageBrowerController.h"
@interface KLHotelDetailViewController ()
@property(nonatomic, strong) STAdvertingScrollView                     *adverView;
@property(nonatomic, strong) STLabel                     *titleLable;
@property(nonatomic, strong) STLabel                     *addLable;
@property(nonatomic, strong) STLabel                     *priceLable;
@property(nonatomic, strong) KLHotleModel                     *detailModel;

@end

@implementation KLHotelDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.model.goods_name;
    [self sendGoodDetailRequest];
    // Do any additional setup after loading the view.
}

#pragma mark --subView
- (void)configSubView{
    UIView * headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 200)];
    headerView.backgroundColor = [UIColor whiteColor];
    
    self.adverView = [[STAdvertingScrollView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, (UIScreenWidth * 0.618)) andWithArray:@[@"banner",@"banner",@"banner",@"banner",@"banner"] handle:^(NSInteger num) {
        
    }];
    self.adverView.imageUrlsArray = self.detailModel.cover_map;
    self.adverView.pageControll.bottom = self.adverView.height - 15;
    self.adverView.pageControll.currentPageIndicatorTintColor = [UIColor whiteColor];
    self.adverView.pageControll.pageIndicatorTintColor = KL_BlueBackGroundColor;
    self.adverView.pageControll.width = self.adverView.dataSouce.count * 10;
    self.adverView.pageControll.centerX = UIScreenWidth / 2;
    self.adverView.canAutoScroll = YES;
    self.adverView.time = 3;
    [headerView addSubview:self.adverView];
    
    self.title = self.detailModel.goods_name;
    
    self.titleLable = [[STLabel alloc] initWithFrame:CGRectMake(10, self.adverView.bottom + 10, UIScreenWidth - 20, 20)
                                                text:self.detailModel.address
                                           textColor:KL_firstTextColor
                                                font:15
                                         isSizetoFit:NO
                                       textAlignment:NSTextAlignmentLeft];
     [NSString lableAutoAdjustheightWithLabel:self.titleLable];
    [headerView addSubview:self.titleLable];
    
    self.addLable = [[STLabel alloc] initWithFrame:CGRectMake(10, self.titleLable.bottom + 10, UIScreenWidth -20, 60)
                                              text:self.detailModel.goods_detail
                                         textColor:KL_thirdTextColor
                                              font:13
                                       isSizetoFit:NO
                                     textAlignment:NSTextAlignmentLeft];
    [headerView addSubview:self.addLable];
    [NSString lableAutoAdjustheightWithLabel:self.addLable];
    self.priceLable = [[STLabel alloc] initWithFrame:CGRectMake(10, self.addLable.bottom + 10, UIScreenWidth - 20, 20)
                                                   text:[NSString stringWithFormat:@"￥%@",_detailModel.price]
                                              textColor:KL_redColor
                                                   font:14
                                            isSizetoFit:NO
                                          textAlignment:NSTextAlignmentLeft];
    [headerView addSubview:self.priceLable];
    


    UIView * sectionHeader = [KLUtils headerViewWithTitle:@"详情"];
    sectionHeader.top = self.priceLable.bottom ;
    sectionHeader.backgroundColor = KL_backgroundColor;
    [headerView addSubview:sectionHeader];
    
    CGFloat top = sectionHeader.bottom +10;
    for (int i =0; i < self.detailModel.synopsis.count; i ++) {
        UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, top, UIScreenWidth - 40, 170)];
        [imageView sd_setImageWithURL:[NSURL URLWithString:self.detailModel.synopsis[i]]];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        
        imageView.clipsToBounds = YES;
        [headerView addSubview:imageView];
        imageView.tag = 10000 + i;
        UITapGestureRecognizer * ges = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onSelectedImageView:)];
        imageView.userInteractionEnabled = YES;
        [imageView addGestureRecognizer:ges];
        top = imageView.bottom + 10;
    }
    headerView.height = top + 10 + 49;
    
    self.tableView.tableHeaderView = headerView;
    [self confiBootomView];
}

- (void)confiBootomView{
    UIView * bootomView = [[UIView alloc] initWithFrame:CGRectMake(0, [KLUtils tabBarTop], UIScreenWidth, 49)];
    [bootomView st_showTopShadow];
    bootomView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bootomView];
    __weak typeof(self) weakSelf =  self;
    STButton * contactButton = [[STButton alloc] initWithFrame:CGRectMake(0, 5, 120, 40)
                                                     title:self.detailModel.telephone
                                                titleColor:[UIColor whiteColor]
                                                 titleFont:15
                                              cornerRadius:5
                                           backgroundColor:KL_BlueBackGroundColor
                                           backgroundImage:nil
                                                     image:nil];
    contactButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    contactButton.right = UIScreenWidth - 20;
    [contactButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedCantactButton];
    }];
    [bootomView addSubview:contactButton];
    
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, bootomView.height)
                                                     text:@"联系电话:"
                                                textColor:KL_firstTextColor
                                                     font:15
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentRight];
    titleLable.right = contactButton.left - 3;
    [bootomView addSubview:titleLable];
}
#pragma mark --Action Method
- (void)onSelectedCantactButton{
    
    NSMutableString* str= [[NSMutableString alloc] initWithFormat:@"telprompt://%@",self.detailModel.telephone];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}
#pragma mark --NetWork Method
- (void)sendGoodDetailRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@",serviceUrl,@"/api/goods/goodshotel"];
    [dic setObject:self.model?self.model.goodID:@"0" forKey:@"goods_id"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 NSDictionary * dataDic =  responseObject[@"data"];
                                               // NSLog(@"json = %@",dataDic.mj_JSONString);
                                                 if ([dataDic.allKeys containsObject:@"info"]) {
                                                     self.detailModel = [KLHotleModel mj_objectWithKeyValues:dataDic[@"info"]];
                                                     [self configSubView];
                                                 }

                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                                 
                                                 
                                             }];
}
#pragma mark -图片查看
- (void)onSelectedImageView:(UIGestureRecognizer*)ges{
    UIImageView * imageView = (id)ges.view;
    NSMutableArray * dataSouce = [NSMutableArray new];
    for (NSString * url in self.detailModel.synopsis) {
        STUrlPhotoModel * model = [STUrlPhotoModel new];
        model.originImageUrl = url;
        model.thumbImageUrl = url;
//        model.originImage = imageView.image;
        [dataSouce addObject:model];
    }
    NSInteger tag = imageView.tag - 10000;
    STPhotoUrlImageBrowerController *  vc = [[STPhotoUrlImageBrowerController alloc] initWithArray:dataSouce curentIndex:tag];
    vc.shouldHideBottomView = YES;
    [self presentViewController:vc animated:NO completion:nil];
}
@end

